package com.uns1066.unsbar.util;

import com.uns1066.unsbar.common.CommonConstant;
import com.uns1066.unsbar.enums.FileType;
import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileUploadUtil
{
  private static String FILE_FOLDER = createTimePath(Calendar.getInstance());
  private static String LOCAL_FILE_FOLDER = CommonConstant.getLocalPath();
  private static String NETWORK_FILE_FOLDER = CommonConstant.getNetworkPath();
  private static String LOCAL_HOST_URL = "";
  private static String VISIT_HOST_URL = "";

  public static Map<String, Object> uploadFile(MultipartFile inFile, Long inFileType, Long userId)
    throws Exception
  {
    Map map = new HashMap();
    String fileType = checkFile(inFile, inFileType);
    String newName = KeyUtil.genKeyUUID();
    String fileName = newName + fileType;

    LOCAL_HOST_URL = LOCAL_FILE_FOLDER + "/" + userId + FILE_FOLDER;
    VISIT_HOST_URL = NETWORK_FILE_FOLDER + "/" + userId + FILE_FOLDER;

    File folder = new File(LOCAL_HOST_URL);

    StringBuffer picPath = new StringBuffer().append(LOCAL_HOST_URL).append(fileName);

    File newFile = new File(picPath.toString());

    if (!folder.exists()) {
      folder.mkdirs();
    }
    FileCopyUtils.copy(inFile.getBytes(), newFile);
    if (inFileType.longValue() != 3L) {
      String zoomUrl = createZoom(newFile, picPath.toString(), fileType, newName, inFileType);
      if (StringUtils.isNotBlank(zoomUrl)) {
        map.put("thumbUrl", CommonConstant.getHostUrl() + zoomUrl);
      }
    }
    map.put("fileUrl", CommonConstant.getHostUrl() + VISIT_HOST_URL + fileName);
    return map;
  }

  private static String checkFile(MultipartFile inFile, Long inFileType)
    throws Exception
  {
    if (inFile.isEmpty()) {
      throw new Exception("file is not null !");
    }
    if (inFileType == null) {
      throw new Exception("fileType is not null !");
    }
    String fileType = inFile.getContentType();

    String oldName = inFile.getOriginalFilename();
    int index = oldName.lastIndexOf(".");
    fileType = oldName.substring(index).toLowerCase();
    if ((fileType == null) || (fileType.equals(""))) {
      throw new Exception("file type is error !");
    }
    String[] type = CommonConstant.getFileType();
    boolean uploadFileTye = SpringUtil.containsElement(type, fileType);

    if (!uploadFileTye) {
      throw new Exception("unknown file type : " + fileType);
    }
    return fileType;
  }

  private static String createTimePath(Calendar calendar)
  {
    int year = calendar.get(1);
    int march = calendar.get(2) + 1;
    return "/" + year + "/" + march + "/";
  }

  public static String createZoom(File newFile, String localFilePath, String fileType, String newFileName, Long inFileType)
    throws Exception
  {
    String zoomUrl = null;
    StringBuffer newFilePath = null; StringBuffer thumbPicturePath = null;
    if (inFileType.intValue() == FileType.FILE_PICTURE.getCode()) {
      newFilePath = new StringBuffer().append(LOCAL_HOST_URL).append(newFileName).append("_s").append(fileType);
      thumbPicturePath = new StringBuffer().append(newFileName).append("_s").append(fileType);
      zoomUrl = createZoomPicture(newFile, newFilePath.toString(), thumbPicturePath.toString());
      thumbPicturePath = new StringBuffer().append(newFileName).append("_x").append(fileType);
      newFilePath = new StringBuffer().append(LOCAL_HOST_URL).append(newFileName).append("_x").append(fileType);
      createZoomPicture(newFile, newFilePath.toString(), thumbPicturePath.toString(), CommonConstant.getXWidth(), CommonConstant.getXHeight());
      thumbPicturePath = new StringBuffer().append(newFileName).append("_m").append(fileType);
      newFilePath = new StringBuffer().append(LOCAL_HOST_URL).append(newFileName).append("_m").append(fileType);
      createZoomPicture(newFile, newFilePath.toString(), thumbPicturePath.toString(), CommonConstant.getMWidth(), CommonConstant.getMHeight());
      thumbPicturePath = new StringBuffer().append(newFileName).append("_mob").append(fileType);
      newFilePath = new StringBuffer().append(LOCAL_HOST_URL).append(newFileName).append("_mob").append(fileType);
      createZoomPicture(newFile, newFilePath.toString(), thumbPicturePath.toString(), CommonConstant.getMobWidth(), CommonConstant.getMobHeight());
    }
    if (inFileType.intValue() == FileType.FILE_VIDEO.getCode()) {
      newFilePath = new StringBuffer().append(LOCAL_HOST_URL).append(newFileName).append("_s").append(".jpg");
      thumbPicturePath = new StringBuffer().append(newFileName).append("_s").append(".jpg");
      zoomUrl = createZoomVideo(localFilePath, newFilePath.toString(), thumbPicturePath.toString());
    }
    return zoomUrl;
  }

  private static String createZoomPicture(File newFile, String localFilePath, String fileName)
    throws Exception
  {
    String zoomPictureUrl = null;
    boolean status = FileUtil.compressPic(newFile, localFilePath, CommonConstant.getFileWidth(), CommonConstant.getFileHeight(), true);
    if (status) {
      zoomPictureUrl = VISIT_HOST_URL + fileName;
    }
    return zoomPictureUrl;
  }

  private static String createZoomPicture(File newFile, String localFilePath, String fileName, int width, int height)
    throws Exception
  {
    String zoomPictureUrl = null;
    boolean status = FileUtil.compressPic(newFile, localFilePath, width, height, true);
    if (status) {
      zoomPictureUrl = VISIT_HOST_URL + fileName;
    }
    return zoomPictureUrl;
  }

  private static String createZoomVideo(String localVideoPath, String newVideoPath, String fileName)
    throws Exception
  {
    String zoomVideoUrl = null;
    boolean status = CaptureImage.processImg(localVideoPath, newVideoPath);
    if (status) {
      zoomVideoUrl = VISIT_HOST_URL + fileName;
    }
    return zoomVideoUrl;
  }
}